package com.lwt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwt.entity.Role;
import com.lwt.entity.RoleExample;
import com.lwt.mapper.RoleMapper;
import com.lwt.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Li Weitong
 * @Date 2021/1/18 16:27
 */
@Service
public class  RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize); // 开启分页
        List<Role> roleList = roleMapper.selectRoleByKeyword(keyword); // 执行查询
        return new PageInfo<>(roleList); // 封装为PageInfo对象返回
    }

    @Override
    public int saveRole(Role role) {
        return roleMapper.insert(role);
    }

    @Override
    public int updateRole(Role role) {

        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void removeRole(List<Integer> roleIdList) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(roleIdList); // delete from t_role where id in(1,2,3)
        roleMapper.deleteByExample(roleExample);
    }
}
